#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
智源数据驱动的内容生成器
"""

import random
from typing import Dict, List, Any, Optional
from zhiyuan_base import ZhiyuanEducationDatabase

class ZhiyuanContentGenerator:
    """基于智源数据的内容生成器"""
    
    def __init__(self):
        self.database = ZhiyuanEducationDatabase()
        self.educational_themes = {
            "emotional_intelligence": {
                "themes": ["情绪认知", "情绪管理", "同理心", "社交技能"],
                "scenarios": ["小动物学会表达情感", "朋友之间的误解与和解"]
            },
            "problem_solving": {
                "themes": ["创造性思维", "逻辑推理", "合作解决", "坚持不懈"],
                "scenarios": ["迷路时找到回家的路", "修复破损的友谊"]
            },
            "moral_values": {
                "themes": ["诚实守信", "善良助人", "勇敢正义", "责任担当"],
                "scenarios": ["诚实承认错误的勇气", "帮助需要帮助的人"]
            }
        }
    
    def generate_story_framework(self, age_group: str, theme: str) -> Dict[str, Any]:
        """生成故事框架"""
        age_content = self.database.get_age_appropriate_content(age_group)
        
        return {
            "age_group": age_group,
            "theme": theme,
            "cognitive_level": age_content["cognitive_stage"],
            "language_level": age_content["language_level"],
            "story_structure": self._build_simple_structure(age_group),
            "characters": self._select_characters(age_group),
            "learning_objectives": self._get_learning_objectives(age_group, theme)
        }
    
    def _build_simple_structure(self, age_group: str) -> List[str]:
        """构建简单故事结构"""
        if age_group == "3-5岁":
            return ["开始", "问题", "解决", "结束"]
        elif age_group == "6-8岁":
            return ["介绍", "冒险开始", "遇到困难", "寻找帮助", "解决问题", "快乐结局"]
        else:
            return ["背景设定", "冲突出现", "尝试解决", "挫折考验", "成长领悟", "问题解决", "价值升华"]
    
    def _select_characters(self, age_group: str) -> Dict[str, str]:
        """选择角色"""
        return {
            "主角": "勇敢善良的小动物",
            "伙伴": "忠诚的朋友",
            "导师": "智慧的长者",
            "挑战者": "需要理解的对手"
        }
    
    def _get_learning_objectives(self, age_group: str, theme: str) -> List[str]:
        """获取学习目标"""
        objectives_map = {
            "3-5岁": {
                "emotional_intelligence": ["认识基本情绪", "学会表达感受"],
                "problem_solving": ["尝试不同方法", "寻求帮助"],
                "moral_values": ["分辨对错", "学会分享"]
            },
            "6-8岁": {
                "emotional_intelligence": ["管理情绪", "理解他人感受"],
                "problem_solving": ["逻辑思考", "团队合作"],
                "moral_values": ["承担责任", "帮助他人"]
            },
            "9-12岁": {
                "emotional_intelligence": ["情绪智能发展", "深度同理心"],
                "problem_solving": ["创新思维", "系统解决"],
                "moral_values": ["价值判断", "社会责任"]
            }
        }
        
        return objectives_map.get(age_group, {}).get(theme, ["综合发展"])
    
    def generate_educational_content(self, framework: Dict[str, Any]) -> Dict[str, Any]:
        """生成教育内容"""
        return {
            "story_outline": self._create_story_outline(framework),
            "interaction_points": self._design_interactions(framework),
            "learning_activities": self._design_activities(framework),
            "assessment_methods": self._create_assessments(framework)
        }
    
    def _create_story_outline(self, framework: Dict[str, Any]) -> List[Dict[str, str]]:
        """创建故事大纲"""
        outline = []
        structure = framework["story_structure"]
        theme = framework["theme"]
        
        for i, stage in enumerate(structure):
            outline.append({
                "stage": stage,
                "content": f"在{stage}阶段，体现{theme}主题的故事情节",
                "duration": "2-3分钟" if framework["age_group"] == "3-5岁" else "3-5分钟"
            })
        
        return outline
    
    def _design_interactions(self, framework: Dict[str, Any]) -> List[Dict[str, Any]]:
        """设计互动点"""
        interactions = []
        age_group = framework["age_group"]
        
        if age_group == "3-5岁":
            interaction_types = ["简单选择", "动作模仿", "声音互动"]
        elif age_group == "6-8岁":
            interaction_types = ["情节选择", "角色扮演", "问题解答"]
        else:
            interaction_types = ["策略选择", "道德判断", "深度思考"]
        
        for i, stage in enumerate(framework["story_structure"]):
            interactions.append({
                "stage": stage,
                "type": interaction_types[i % len(interaction_types)],
                "purpose": "促进参与和学习"
            })
        
        return interactions
    
    def _design_activities(self, framework: Dict[str, Any]) -> List[Dict[str, str]]:
        """设计学习活动"""
        activities = []
        objectives = framework["learning_objectives"]
        
        for objective in objectives:
            activities.append({
                "objective": objective,
                "activity": f"通过故事互动实现{objective}的学习目标",
                "method": "游戏化学习"
            })
        
        return activities
    
    def _create_assessments(self, framework: Dict[str, Any]) -> Dict[str, List[str]]:
        """创建评估方法"""
        age_group = framework["age_group"]
        
        if age_group == "3-5岁":
            methods = ["观察记录", "游戏表现", "互动反应"]
        elif age_group == "6-8岁":
            methods = ["作品展示", "口头表达", "同伴评价"]
        else:
            methods = ["项目成果", "论述表达", "批判分析"]
        
        return {
            "formative": methods,
            "summative": ["综合表现评价", "学习成果展示"],
            "self_assessment": ["简单反思"] if age_group != "3-5岁" else []
        }